<?php 

/*
 * page callback
*/
function userextra_overview_provinces(){
	$header = array(t('#Code'), t('Province name'), t('Operator'));
	$rows = array();
	
	$province_list = get_province_list();

	foreach ($province_list as $key => $province){
		    $rows[] = array(
		    		
		    			$province -> province_code,
		    			$province -> province_name, 
	    				t('<a href="@link">Edit</a>', array('@link' => url('admin/structure/provinces/add')))
					    );
	}

	$build['province_table'] = array(
			'#theme' => 'table',
			'#header' => $header,
			'#rows' => $rows,
			'#empty' => t('No Provinces available. <a href="@link">Add province</a>.', array('@link' => url('admin/structure/provinces/add'))),
	);
	return $build;
}

function userextra_add_province_form(){
	$form['province_code'] = array(
			'#title' => t('Province code'),
			'#type' => 'textfield',
			'#size' => 15,
			'#description' => t(''),
			'#required'		=> TRUE,
	);

	$form['province_name'] = array(
			'#title' => t('Province name'),
			'#type' => 'textfield',
			'#size' => 25,
			'#description' => t(''),
			'#required'		=> TRUE,
	);

	$form['submit'] = array(
			'#type' => 'submit',
			'#value' => t('Save'),
	);
	return $form;
}

function userextra_add_province_form_submit($form, &$form_state){
	$nid = db_insert('provinces')
	->fields(array(
			'province_code' 			=> 	$form_state['values']['province_code'],
			'province_name' 			=> 	$form_state['values']['province_name'],
	))
	->execute();
}

function get_province_list(){
	$result = db_query('SELECT province_name, province_code FROM {provinces} ORDER BY province_name ASC');
	return $result->fetchAll();	
}

?>